<?php
/**
 * Created by PhpStorm.
 * User: 刘红波 <liuhb@cqjcdflzxsws.com>
 * Date: 2018/7/31
 * Time: 16:04
 */

namespace app\api\controller;


class WeUserCategory extends Base
{
    public function index(){
        $uid = $this->uid;
        if(!$uid){
            list($this->code,$this->msg,$this->data) = [10000,'请登录',[]];
            return;
        }else{
            $query = $this->request->post();
            $flag = isset($query['flag'])?$query['flag']:false;
            $data = \app\api\model\WeUserCategory::userCate($uid,$flag);
            if ($flag){
                $pubCate = \app\api\model\WeCategory::pubCate(true);
                array_unshift($data,$pubCate);
            }
            list($this->code,$this->msg,$this->data) = [1,'关注领域请求成功',$data];
            return;
        }
    }

    /**
     * 添加关注
     */
    public function focus(){
        $data = $this->request->post();
        $res = $this->validate($data,'WeUserCate');
        if(true !==$res){
            list($this->code,$this->msg) = [0,$res];
            return;
        }
        //判断领域是否存在
        $cateInfo = \app\api\model\WeCategory::where(['id'=>$data['category_id']])->find();{
            if (empty($cateInfo)){
                list($this->code,$this->msg) = [0,'关注失败,不存在领域'];
                return;
            }elseif($cateInfo->status == 0 || $cateInfo->is_delete == 1){
                list($this->code,$this->msg) = [0,'关注失败,领域已失效'];
                return;
            }
        }
        $saveData = [
            'category_id'=>$data['category_id'],
            'user_id'=>$this->uid
        ];
        //判断是否已关注过
        $exists = \app\api\model\WeUserCategory::where($saveData)->find();
        if (empty($exists)){
            $result = \app\api\model\WeUserCategory::create($saveData);
        }else{
            $result = $exists->save(['is_delete'=>0],$saveData);
        }
        if(empty($result) && !$result){
            list($this->code,$this->msg) = [0,'关注失败'];
            return;
        }else{
            list($this->code,$this->msg) = [1,'关注领域'.$cateInfo->name.'成功'];
        }
    }


    /**
     * 取关
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cancelFocus(){
        $data = $this->request->post();
        $res = $this->validate($data,'WeUserCate');
        if(true !==$res){
            list($this->code,$this->msg) = [0,$res];
            return;
        }
        $saveData = [
            'category_id'=>$data['category_id'],
            'user_id'=>$this->uid
        ];
        //判断是否已关注过
        $exists = \app\api\model\WeUserCategory::where($saveData)->find();
        if(empty($exists)){
            list($this->code,$this->msg) = [1,'取关成功'];
            return;
        }else{
            $result = $exists->save(['is_delete'=>1]);
            if($result){
                list($this->code,$this->msg) = [1,'取关成功'];
                return;
            }else{
                list($this->code,$this->msg) = [0,'取关失败'];
            }
        }
    }
}